package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
<#if swagger2??>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
</#if>
<#if cfg.securityEnable??>
import org.springframework.security.access.prepost.PreAuthorize;
</#if>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.paw.common.annotation.SysLog;
import com.paw.common.util.CommonResult;
import com.paw.common.util.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
<#if swagger2??>
@Api(tags = "${table.controllerName}", value = "${table.comment}管理")
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
 @Slf4j
public class ${table.controllerName} {
</#if>

<#assign serviceName=table.serviceName?substring(1)?uncap_first >
    @Autowired
    private ${table.serviceName} ${serviceName};

    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "根据条件查询所有${table.comment}列表")
    @ApiOperation("根据条件查询所有${table.comment}列表")
    @GetMapping(value = "/")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:read')")
</#if>
    public Object get${entity}ByPage(${entity} entity,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
      try {
        return new CommonResult().success(${serviceName}.page(new Page<${entity}>(pageNum, pageSize), new QueryWrapper<>(entity)));
      } catch (Exception e) {
        log.error("根据条件查询所有${table.comment}：{}", e.getMessage(), e);
      }
      return new CommonResult().failed();
    }

    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "保存${table.comment}")
    @ApiOperation("保存${table.comment}表")
    @PutMapping(value = "/")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:add')")
</#if>
    public Object save${entity}(@RequestBody ${entity} entity) {
      try {
        if (${serviceName}.save(entity)) {
          return new CommonResult().success();
        }
      } catch (Exception e) {
        log.error("保存${table.comment}表：{}", e.getMessage(), e);
        return new CommonResult().failed();
      }
      return new CommonResult().failed();
    }

    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "更新${table.comment}")
    @ApiOperation("更新${table.comment}")
    @PostMapping(value = "/{id}")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:update')")
</#if>
    public Object update${entity}(@RequestBody ${entity} entity, @PathVariable Long id) {
<#list table.fields as field>
    <#if field.keyFlag>
        <#assign keyPropertyName="${field.propertyName}"/>
    </#if>
</#list>
<#assign setPkMethod="set"+keyPropertyName?cap_first>
    try {
        // 设置更新的主键
        entity.${setPkMethod}(id);
        if (${serviceName}.updateById(entity)) {
          return new CommonResult().success();
        }
      } catch (Exception e) {
        log.error("更新${table.comment}：{}", e.getMessage(), e);
        return new CommonResult().failed();
      }
      return new CommonResult().failed();
    }

    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "删除${table.comment}")
    @ApiOperation("删除${table.comment}")
    @GetMapping(value = "/delete/{id}")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:delete')")
</#if>
    public Object delete${entity}(@ApiParam("${table.comment}id") @PathVariable Long id) {
     try {
       if (ValidatorUtils.empty(id)) {
         return new CommonResult().paramFailed("${table.comment}id");
       }
       if (${serviceName}.removeById(id)) {
         return new CommonResult().success();
       }
     } catch (Exception e) {
       log.error("删除${table.comment}：{}", e.getMessage(), e);
       return new CommonResult().failed();
     }
      return new CommonResult().failed();
    }

    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "查询${table.comment}")
    @ApiOperation("查询${table.comment}")
    @GetMapping(value = "/{id}")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:read')")
</#if>
    public Object get${entity}ById(@ApiParam("${table.comment}id") @PathVariable Long id) {
     try {
        if (ValidatorUtils.empty(id)) {
           return new CommonResult().paramFailed("${table.comment}id");
        }
        ${entity} coupon = ${serviceName}.getById(id);
           return new CommonResult().success(coupon);
       } catch (Exception e) {
          log.error("查询${table.comment}：{}", e.getMessage(), e);
          return new CommonResult().failed();
       }
    }

    @ApiOperation(value = "批量删除${table.comment}")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    @SysLog(MODULE = "${package.ModuleName!}", REMARK = "批量${table.comment}")
<#if cfg.securityEnable??>
    @PreAuthorize("hasAuthority('${package.ModuleName}:${table.entityPath}:delete')")
</#if>
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean count = ${serviceName}.removeByIds(ids);
        if (count) {
           return new CommonResult().success(count);
        } else {
           return new CommonResult().failed();
        }
     }

}
</#if>
